////////////////////////////////////////////////////////////////////////////////
// Copyright 2010 Michael Schmalle - Teoti Graphix, LLC
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
// http://www.apache.org/licenses/LICENSE-2.0 
// 
// Unless required by applicable law or agreed to in writing, software 
// distributed under the License is distributed on an "AS IS" BASIS, 
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and 
// limitations under the License
// 
// Author: Michael Schmalle, Principal Architect
// mschmalle at teotigraphix dot com
////////////////////////////////////////////////////////////////////////////////

package org.teotigraphix.swizplugin.bundles
{

import org.swizframework.core.ISwiz;
import org.teotigraphix.swizplugin.events.BundleEvent;
import org.teotigraphix.swizplugin.services.IBundleService;

/**
 * @private
 * 
 * @author Michael Schmalle
 * @copyright Teoti Graphix, LLC
 * @productversion 1.0
 */
public class BundleProxy extends Bundle
{
	//--------------------------------------------------------------------------
	//
	//  Public :: Propeties
	//
	//--------------------------------------------------------------------------
	
	//----------------------------------
	//  swiz
	//----------------------------------
	
	/**
	 * @private
	 */
	private var _swiz:ISwiz;
	
	/**
	 * The containing <code>ISwiz</code> instance.
	 */
	public function get swiz():ISwiz
	{
		return _swiz;
	}
	
	/**
	 * @private
	 */	
	public function set swiz(value:ISwiz):void
	{
		_swiz = value;
	}
	
	//--------------------------------------------------------------------------
	//
	//  Constructor
	//
	//--------------------------------------------------------------------------
	
	/**
	 * @private
	 */
	public function BundleProxy(bundleData:BundleData, 
								bundleService:IBundleService,
								swiz:ISwiz)
	{
		super(bundleData, bundleService);
		
		_swiz = swiz;
	}
	
	//--------------------------------------------------------------------------
	//
	//  Overridden Protected :: Methods
	//
	//--------------------------------------------------------------------------
	
	/**
	 * Calls SwizContext.load().
	 * 
	 * @throws BundleError Bundle not resolved
	 */
	override protected function loadContext(options:int):void
	{
		var loading:Boolean = false;
		
		// if the framework is not active OR the bundle's is ACTIVE
		if (/*!framework.active || */(state & BundleState.ACTIVE) != 0)
			return;
		
		// state has to be STARTING
		if (state == BundleState.INSTALLED) 
		{
			resolve();
			var resolved:Boolean = true;
			if (!resolved)
				throw new BundleError("Bundle not resolved");
			//if (resolveBundles())
			//throw new Error("Bundle INSTALLED");
		}
		
		_context = getContext();
		
		// if I put a try {} around this I would know if the activator
		// failed to load, then I could say this
		
		try {
			SwizContext(_context).load();
		} catch (eo:TypeError) {
			// TypeError: Error #1007
			loading = true;
		} catch (e:BundleError) {
			
			state = BundleState.STOPPING;
			
			dispatchEvent(BundleEvent.BUNDLE_STOPPING);
			
			context.close();
			_context = null;
			
			state = BundleState.RESOLVED;
			
			dispatchEvent(BundleEvent.BUNDLE_STOPPED);
			
			trace("BundleError::originalError [" + e.originalError.message + "]" );
			trace("");
			trace(e.originalError.getStackTrace());
			trace("");
			throw e;
		} catch (ee:Error) {
			loading = true;
		} finally {
			
		}
		
		if (state == BundleState.UNINSTALLED)
		{
			context.close()
			_context = null;
			// throw()
		}
	}
	
	/**
	 * Calls SwizContext.start().
	 */
	override protected function startupContext(options:int):void
	{
		if (state & BundleState.ACTIVE != 0)
			return;
		
		state = BundleState.STARTING;
		
		dispatchEvent(BundleEvent.BUNDLE_STARTING);
		
		SwizContext(_context).start();
		
		bundleStarted();
	}
	
	//--------------------------------------------------------------------------
	//
	//  Protected :: Methods
	//
	//--------------------------------------------------------------------------
	
	/**
	 * @private
	 */
	protected function createContext():SwizContext
	{
		return new SwizContext(this, swiz);
	}
	
	/**
	 * @private
	 */
	protected function getContext():SwizContext
	{
		if (_context == null) 
		{
			if ((state & (BundleState.STARTING | BundleState.RESOLVED | 
				BundleState.ACTIVE | BundleState.STOPPING)) != 0)
			{
				_context = createContext();
			}
		}
		return _context as SwizContext;
	}
	
	/**
	 * @private
	 */
	private function bundleStarted():void
	{
		//		if (framework.active)
		//		{
		
		state = BundleState.ACTIVE;
		
		dispatchEvent(BundleEvent.BUNDLE_STARTED);
		//		}
	}
}
}